#include "mouse.h"
#include "ui_mouse.h"
#include "QDebug"
#include "QMouseEvent"
#include "QMouseEventTransition"
#include "QEvent"
#include "QLabel"

mouse::mouse(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mouse)
{
    ui->setupUi(this);


}

//重载鼠标进去事件
void mouse:: enterEvent(QEvent *event){
    qDebug()<<"鼠标进入了";
}
//重载鼠标离开事件
void mouse:: leaveEvent(QEvent *event){
    qDebug()<<"鼠标离开了";
}
//重载鼠标按下
void mouse:: mousePressEvent(QMouseEvent *event){
    if(event->button()== Qt::LeftButton){
        //打印鼠标按下位置
        QString str = QString("鼠标左键按下了了  x = %1   y = %2  global_x = %3 global_y = %4").arg(event->x()).arg(event->y()).arg(event->globalX()).arg(event->globalY());
        qDebug() << str;
    }else{
        QString str = QString("鼠标右键按下了了  x = %1   y = %2  global_x = %3 global_y = %4").arg(event->x()).arg(event->y()).arg(event->globalX()).arg(event->globalY());
        qDebug() << str;
    }

}
//重载鼠标释放
void mouse:: mouseRealeaseEvent(QMouseEvent *event){
    qDebug() << "鼠标释放了";
}
//重载鼠标移动
void mouse:: mouseMoveEvent(QMouseEvent *event){
    QString str = QString("鼠标移动  x = %1   y = %2  global_x = %3 global_y = %4").arg(event->x()).arg(event->y()).arg(event->globalX()).arg(event->globalY());
    qDebug() << str;
}
//事件拦截（仅在event返回false时才进入子类event）
bool mouse::event(QEvent *e){
    //拦截鼠标按下的操作，使在event函数中执行，不在进入mouseEvent
    if(e->type()==QEvent::MouseButtonPress){
        /*event->x()中event为QMouseEvent，而e为QEvent类（父类），需转化*/
        QMouseEvent *event = static_cast<QMouseEvent *>(e);
        QString str = QString("鼠标在event函数中按下了了  x = %1   y = %2  global_x = %3 global_y = %4").arg(event->x()).arg(event->y()).arg(event->globalX()).arg(event->globalY());
        qDebug() << str;
        return true;//拦截
    }
    return QWidget::event(e);
}

mouse::~mouse()
{
    delete ui;
}
